package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotSchoolHeadteacher;
import com.ruoyi.wisdom.domain.swap.SwapSchoolHeadteacher;
import com.ruoyi.wisdom.service.IIotSchoolHeadteacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolHeadteacherEvent extends IotEvent {

    @Autowired
    private IIotSchoolHeadteacherService iotSchoolHeadteacherServic;

    /**
     * 处理
     *
     * @param swapSchoolHeadteacher
     */
    public void handle(SwapSchoolHeadteacher swapSchoolHeadteacher) {
        if (swapSchoolHeadteacher.getOperate() == null) {
            save(swapSchoolHeadteacher);
        } else {
            switch (swapSchoolHeadteacher.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapSchoolHeadteacher);
                    break;
                default:
                    save(swapSchoolHeadteacher);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapSchoolHeadteacher
     */
    public void save(SwapSchoolHeadteacher swapSchoolHeadteacher) {
        IotSchoolHeadteacher result = iotSchoolHeadteacherServic.selectIotSchoolHeadteacherById(swapSchoolHeadteacher.getId());
        // 实体装换
        IotSchoolHeadteacher iotSchoolHeadteacher = getModelMapper().map(swapSchoolHeadteacher, IotSchoolHeadteacher.class);
        if (result != null) {
            iotSchoolHeadteacher.setId(swapSchoolHeadteacher.getId());
            iotSchoolHeadteacherServic.updateIotSchoolHeadteacher(iotSchoolHeadteacher);
        } else {
            iotSchoolHeadteacherServic.insertIotSchoolHeadteacher(iotSchoolHeadteacher);
        }
    }

    /**
     * 删除
     *
     * @param swapSchoolHeadteacher
     */
    public void delete(SwapSchoolHeadteacher swapSchoolHeadteacher) {
        iotSchoolHeadteacherServic.deleteIotSchoolHeadteacherById(swapSchoolHeadteacher.getId());
    }
}
